import { Space, Tag, type TableProps, Button, message, Popconfirm, Table,Modal } from 'antd';
import { useState } from 'react';

interface DataType {
  key: string;
  name: string;
  role: number;
  desc: string;
  tags: string[];
}


import React from 'react'
import FormList from '../user/FormList';

export default function column() {
  const [data,setdata]=useState<DataType[]>([
    {
      key: '1',
      name: '徐小夕',
      role: 1,
      desc: 'H5-Dooring作者, 掘金签约作者，知乎专栏作家',
      tags: ['前端工程师', 'developer'],
    },
    {
      key: '2',
      name: '张三',
      role: 2,
      desc: '知乎专栏作家',
      tags: ['前端工程师', 'developer', 'Dooring'],
    },
    {
      key: '3',
      name: '李盟',
      role: 2,
      desc: 'Dooring共建者',
      tags: ['后端工程师', 'V6.Dooring', 'Dooring'],
    },
    {
      key: '4',
      name: '王阿明',
      role: 2,
      desc: '技术合伙人',
      tags: ['全栈工程师', '橙子表单'],
    },
    {
      key: '5',
      name: '张小明',
      role: 2,
      desc: '技术合伙人',
      tags: ['全栈工程师', '橙子表单'],
    },
    {
      key: '6',
      name: 'Tom',
      role: 2,
      desc: '技术合伙人',
      tags: ['全栈工程师', 'Next-Admin'],
    },
    {
      key: '7',
      name: 'Json',
      role: 2,
      desc: '技术合伙人',
      tags: ['全栈工程师', 'Next-Admin'],
    },
  ])
  // 文字标识
  let [title,setTitle]=useState('添加')
  // 数据回显
  let [itemobj,setItemobj]=useState({})
  //弹窗控制
  const [isModalOpen, setIsModalOpen] = useState(false);
  const [currentItem, setCurrentItem] = useState<DataType | null>(null);
  const showModal = () => {
    setIsModalOpen(true);
  };

  const handleOk = () => {
    setIsModalOpen(false);
  };

  const handleCancel = () => {
    setIsModalOpen(false);
  };
  const handleFormSuccess = (values: any, isEdit: boolean) => {
    if (isEdit) {
      // 编辑逻辑
      setdata(data.map(item => 
        item.key === currentItem?.key ? { ...item, ...values, key: currentItem.key } : item
      ))
      message.success('编辑成功')
    } else {
      // 添加逻辑
      const newItem = {
        "key": Date.now().toString(),
        ...values
      };
      setdata([...data, newItem])
      message.success('添加成功')
    }
    setIsModalOpen(false);
    setCurrentItem(null);
  };
  // 表头设置
  const columns: TableProps<DataType>['columns'] = [
    {
      title: '用户名',
      dataIndex: 'name',
      key: 'name',
      render: (text) => <a>{text}</a>,
      fixed: 'left',
      width: 100
    },
    {
      title: '角色',
      dataIndex: 'role',
      key: 'role',
      render: role => role === 1 ? '超级管理员' : '开发者'
    },
    {
      title: '简介',
      dataIndex: 'desc',
      key: 'desc',
    },
    {
      title: '标签',
      key: 'tags',
      dataIndex: 'tags',
      render: (_, { tags }) => (
        <>
          {tags.map((tag) => {
            let color = tag.length > 5 ? 'geekblue' : 'green';
            if (tag === 'loser') {
              color = 'volcano';
            }
            return (
              <Tag color={color} key={tag}>
                {tag.toUpperCase()}
              </Tag>
            );
          })}
        </>
      ),
    },
    {
      title: '操作',
      key: 'action',
      render: (_, record) => (
        <Space size="middle">
          <a  onClick={()=>{
        showModal()
        setTitle('编辑')
        setItemobj(record)
      }}>编辑</a>
          <Popconfirm
            title="删除"
            description="确定删除吗?"
            onConfirm={() => {
              let newData= data.filter(item => {
                return item.key != record.key
              })
              setdata(newData)
            }}
            okText="确定"
            cancelText="取消"
          >
            <Button danger>删除</Button>
          </Popconfirm>
        </Space>
      ),
    },
  ];
  return (
    <div>
      <Button type="primary" onClick={()=>{
        showModal()
        setTitle('添加')
        setItemobj({})
      }}>
        添加
      </Button>
      <Modal footer={null} title={title} open={isModalOpen} onOk={handleOk} onCancel={handleCancel}>
        <FormList 
        title={title} 
        onCancel={handleCancel} 
        key={new Date().getTime()} 
        itemobj={itemobj}
        onSuccess={handleFormSuccess}/>
      </Modal>
      <Table columns={columns} dataSource={data} pagination={{ pageSize: 5 }} scroll={{ x: 1000 }} />
    </div>
  )
}
